package com.ruoyi.mushcoin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.mushcoin.domain.bo.TgUserInfoBo;
import com.ruoyi.mushcoin.domain.vo.TgUserInfoImportVo;
import com.ruoyi.mushcoin.domain.vo.TgUserInfoVo;
import com.ruoyi.mushcoin.listener.TgUserInfoImportListener;
import com.ruoyi.mushcoin.service.ITgUserInfoService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * TG用户Controller
 *
 * @author billy
 * 2024-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mushcoin/userInfo")
public class TgUserInfoController extends BaseController {
    @Resource
    private ITgUserInfoService tgUserInfoService;


    @SaCheckPermission("mushcoin:userInfo:list")
    @GetMapping("/inviteUserRewardsView")
    public TableDataInfo<TgUserInfoVo> inviteUserRewardsView(TgUserInfoBo tgUserInfoBo) {
        return tgUserInfoService.inviteUserRewardsPage(tgUserInfoBo);
    }
    @SaCheckPermission("mushcoin:userInfo:list")
    @GetMapping("/inviteDataView")
    public TableDataInfo<TgUserInfoVo> inviteDataView(TgUserInfoBo tgUserInfoBo) {
        return list(tgUserInfoBo);
    }
    @SaCheckPermission("mushcoin:userInfo:list")
    @GetMapping("/gameDataView")
    public TableDataInfo<TgUserInfoVo> gameDataView(TgUserInfoBo tgUserInfoBo) {
        return list(tgUserInfoBo);
    }


    /**
     * 查询TG用户列表
     */
    @SaCheckPermission("mushcoin:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TgUserInfoVo> list(TgUserInfoBo tgUserInfoBo) {
        return tgUserInfoService.selectPage(tgUserInfoBo);
    }

    /**
     * 导出TG用户列表
     */
    @SaCheckPermission("mushcoin:userInfo:export")
    @Log(title = "TG用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TgUserInfoBo tgUserInfoBo) {
        List<TgUserInfoVo> list = tgUserInfoService.selectList(tgUserInfoBo);
        ExcelUtil.exportExcel(list, "TG用户", TgUserInfoVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "TG用户", businessType = BusinessType.IMPORT)
    @SaCheckPermission("mushcoin:userInfo:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TgUserInfoImportVo> result = ExcelUtil.importExcel(file.getInputStream(), TgUserInfoImportVo.class, new TgUserInfoImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("mushcoin:userInfo:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "TG用户", TgUserInfoImportVo.class, response);
    }

    /**
     * 获取TG用户详细信息
     */
    @SaCheckPermission("mushcoin:userInfo:query")
    @GetMapping(value = "/{id}")
    public R<TgUserInfoVo> getInfo(@PathVariable Long id) {
        return R.ok(tgUserInfoService.selectById(id));
    }

    /**
     * 新增TG用户
     */
    @SaCheckPermission("mushcoin:userInfo:add")
    @Log(title = "TG用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody TgUserInfoBo tgUserInfoBo) {
        boolean inserted = tgUserInfoService.insert(tgUserInfoBo);
        if (!inserted) {
            return R.fail("新增TG用户记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改TG用户
     */
    @SaCheckPermission("mushcoin:userInfo:edit")
    @Log(title = "TG用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody TgUserInfoBo tgUserInfoBo) {
        Boolean updated = tgUserInfoService.update(tgUserInfoBo);
        if (!updated) {
            return R.fail("修改TG用户记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除TG用户
     */
    @SaCheckPermission("mushcoin:userInfo:remove")
    @Log(title = "TG用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = tgUserInfoService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除TG用户记录失败!");
        }
        return R.ok();
    }
}
